package com.fll.fangyishare.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fll.fangyicommon.entity.ResultOk;
import com.fll.fangyicommon.entity.share.Collect;
import com.fll.fangyicommon.entity.share.Liked;
import com.fll.fangyicommon.entity.share.Share;
import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyishare.service.CollectService;
import com.fll.fangyishare.service.LikedService;
import com.fll.fangyishare.service.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author fll
 * create 2023/3/26
 */
@RestController
@Slf4j
@RequestMapping("/share")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private ShareService shareService;

    //登录用户收藏某个share
    @PostMapping("/addCollect")
    public ResultOk addCollect(@RequestBody Map<String, String> params, @RequestHeader String userInfo) {
        ResultOk ok = new ResultOk<>();
        Integer shareId = Integer.parseInt(params.get("shareId"));
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }

        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            Boolean isCollect = collectService.isCollect(user.getUserId(), shareId);
            if (isCollect){
                ok.setCode(400);
                ok.setMsg("已经收藏过啦");
                return ok;
            }
            //没有收藏则添加记录
            Collect collect = new Collect();
            collect.setUserId(user.getUserId());
            collect.setShareId(shareId);
            Integer num = collectService.addCollect(collect);
            if (num != -1){
                ok.setCode(200);
                ok.setMsg("收藏成功");
                return ok;
            }
            ok.setCode(400);
            ok.setMsg("收藏失败");
            return ok;
        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端异常");
            return ok;
        }
    }

    //登录用户取消收藏某个share
    @PostMapping("/cancelCollect")
    public ResultOk cancelCollect(@RequestBody Map<String, String> params, @RequestHeader String userInfo) {
        ResultOk ok = new ResultOk<>();
        Integer shareId = Integer.parseInt(params.get("shareId"));
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }
        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            Collect collect = new Collect();
            collect.setUserId(user.getUserId());
            collect.setShareId(shareId);
            Integer num = collectService.cancelCollect(collect);
            if(num != -1){
                ok.setCode(200);
                ok.setMsg("取消成功");
            }else {
                ok.setCode(400);
                ok.setMsg("取消失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端异常");
        }
        return ok;
    }

    //获取登录用户所有收藏的share
    @GetMapping("/getCollectByUser")
    public ResultOk getCollectByUser(@RequestHeader String userInfo) {
        ResultOk<List<Share>> ok = new ResultOk<>();
        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            //所有收藏的shareId     eg：(1,2,3....)
            String shareIds = collectService.getCollectByUser(user.getUserId());
            if (!shareIds.equals(")")){ //==")"则为空
                List<Share> likedShareList = shareService.getCollectShareByUser(user.getUserId(), shareIds);
                ok.setCode(200);
                ok.setMsg("查询成功");
                ok.setData(likedShareList);
            } else {
                ok.setCode(400);
                ok.setMsg("暂无收藏的share");
            }
        } catch (IOException e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("查询失败");
            return ok;
        }
        return ok;
    }

}
